package com.example.megaandroidrssreader;

import java.io.FileNotFoundException;
import java.io.IOException;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

public abstract class BaseActivity extends Activity {

	public static final String PACKAGE_NAME = "com.example.megaandroidrssreader";
	
	protected AppSettings mAppSettings;
	protected RssReaderDao mRssReaderDao;
	
	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		mAppSettings = AppSettings.newInstance();
		mRssReaderDao = new RssReaderDaoImpl(this.getApplicationContext());

		if (mRssReaderDao.getViewModeSetting() != 0) {
			mAppSettings.mViewMode = mRssReaderDao.getViewModeSetting();
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		MenuItem viewModeMenuItem = (MenuItem)menu.findItem(R.id.action_view_mode);
		if (mAppSettings.mViewMode == AppSettings.VIEW_MODE_HTML) {
			viewModeMenuItem.setTitle("HTML");
		} else if (mAppSettings.mViewMode == AppSettings.VIEW_MODE_PLAIN_TEXT) {
			viewModeMenuItem.setTitle("Plain text");
		} else {
			viewModeMenuItem.setTitle("Error : " + mAppSettings.mViewMode);
		}

		return super.onPrepareOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
			case R.id.action_view_mode :
				if (mAppSettings.mViewMode == AppSettings.VIEW_MODE_HTML) {
					mAppSettings.mViewMode = AppSettings.VIEW_MODE_PLAIN_TEXT;
				} else if (mAppSettings.mViewMode == AppSettings.VIEW_MODE_PLAIN_TEXT) {
					mAppSettings.mViewMode = AppSettings.VIEW_MODE_HTML;
				}
				mRssReaderDao.setViewModeSetting(mAppSettings.mViewMode);
				
				this.onUpdateViewMode();
				this.invalidateOptionsMenu();
				return true;
			default:
				return super.onOptionsItemSelected(item);
		}
	}

	@Override
	protected void onResume() {
		super.onResume();
		this.invalidateOptionsMenu();
	}
	
	protected void onUpdateViewMode() {
	}
	
}
